"""
@Project:${2108A}人工智能
@File:force_login.py
@Author:柴奇峰
@Date:22:01
"""
from rest_framework.views import Response
from utils.jwt_token import verification_token
from users.models import UserModel

def check_login(func):
    def wrapper(self,request,*args,**kwargs):
        token = request.headers.get('token')
        print('装饰器接收前端头部的token',token)

        payload = verification_token(token)
        print('装饰器解码后的token',payload)

        if type(payload) == dict:
            id = payload.get('id')
            user = UserModel.objects.get(id=id)
            request.user = user
            return func(self,request,*args,**kwargs)
        else:
            return Response({'code':502,'msg':'未登录'})
    return wrapper







